<?php

namespace App\Http\Controllers\Admin;

use App\Model\Survey;
use App\Model\SurveyCate;
use App\Model\SurveyColleague;
use App\Model\SurveyMemberAnswer;
use App\Model\SurveyQuestion;
use App\Model\SurveyQuestionOption;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class SurveyController extends Controller
{
    /**
     * 问卷调查展示页
     */
    public function index()
    {
        $surveys =  Survey::orderBy('create_at','desc')->get()->toArray();
        $cates = SurveyCate::all();
//        dd($surveys);
        return view('admin.survey.index', ['surveys' => $surveys, 'cates' => $cates]);
    }

    /**
     * 问卷详情页
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail($id)
    {
        $survey = Survey::find($id)->toArray();
        $questions = SurveyQuestion::where('survey_id', $id)->get();
        $question_ids = array_map(function($e){
            return $e['id'];
        }, $questions->toArray());
        $options = SurveyQuestionOption::whereIn('question_id', $question_ids)->get();
//        dd($survey);
        $cates = SurveyCate::all();
        return view('admin.survey.detail', [
            'survey' => $survey,
            'questions' => $questions,
            'options' => $options,
            'cates' => $cates
        ]);
    }

    /**
     * 问卷统计图表页
     * @param $id
     */
    public function chart($id)
    {
        $survey = Survey::find($id);    //问卷信息
        if($survey->cate_id != 2){
            abort(404);
        }

        $dpt = SurveyColleague::select('*', DB::raw('count(id) as nums'))->where('survey_id', $id)->groupBy('dpt')->get();  //按部门人数信息
        $nums = SurveyColleague::where('survey_id', $id)->count();  //参与问卷总人数
        $questions  = SurveyQuestion::where('survey_id', $id)->get();   //该问卷的所有提问
        $answers = SurveyMemberAnswer::where('survey_id', $id)->with('colleague')->get();  //参与该问卷的用户答案
        $question_ids = array_map(function($e){
            return $e['id'];
        }, $questions->toArray());
        $options = SurveyQuestionOption::whereIn('question_id', $question_ids)->get();  //找到问题的所有选项

        $optionCounts = DB::select('SELECT a.*, b.title, c.dpt, count(a.id) "nums" FROM vote_survey_member_answers a LEFT JOIN vote_survey_questions b ON a.question_id = b.id LEFT JOIN vote_survey_colleagues c ON a.survey_member_id = c.id WHERE a.survey_id = ? AND b.type != "textarea" GROUP BY a.question_id, a.option_id, c.dpt', [$id]);
        ;  //答案分组
        $tmp = DB::select('SELECT a.*, b.title FROM vote_survey_question_options a LEFT JOIN vote_survey_questions b ON a.question_id = b.id WHERE b.survey_id = ?', [$id]);
        $optionCounts = json_decode(json_encode($optionCounts), true);
        $tmp = json_decode(json_encode($tmp), true);

        $new = array_map(function($e) use ($optionCounts, $dpt){
            $data = [];
            foreach ($dpt as $d){
                $data[$d->dpt] = 0;
            }
            foreach ($optionCounts as $optionCount)
            {
                if($optionCount['question_id'] == $e['question_id'] && $optionCount['option_id'] == $e['id']){
                    $data[$optionCount['dpt']] = $optionCount['nums'];
                }
            }
            $e['data'] = $data;
            return $e;

        }, $tmp);
//        dd($new);

//        dd($optionCounts);
        return view('admin.survey.chart', [
            'survey' => $survey,
            'dpt' => $dpt,
            'nums' => $nums,
            'questions' => $questions,
            'answers' => $answers,
            'options' => $options,
            'optionCounts' => $new
        ]);
    }
}
